//_OpenScript( "includes/defines.lua" )

DECAL_SCRATCH3 = "MahackCut" //74 --thanks andyvincent - http://forum.facepunchstudios.com/showthread.php?t=62384 (only viewable to goldmembers)
DECAL_BLOOD1 = 20
bloody = false
//swung = false
//justbloodied = false
//local modelDefined = false

if (SERVER) then
	AddCSLuaFile( "shared.lua" )
end

if (CLIENT) then
//	local MySelf = LocalPlayer()
	SWEP.PrintName = "Sabre"
	SWEP.Slot = 0
	SWEP.SlotPos = 1
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = false
	
	killicon.Add("weapon_sabre", "gmod/SWEP/sabre_select", Color(255,255,255,255))
end

SWEP.Author = "PirateShip Wars GM9 / Metroid48"
SWEP.Contact = "metroid48@gmail.com"
SWEP.Instructions = ""
SWEP.Spawnable = true
SWEP.HoldType = "melee"

SWEP.AutoSwitchTo = true

SWEP.ViewModel = "models/sabre/v_sabre.mdl"

SWEP.WorldModel = "models/sabre/w_sabre.mdl"

SWEP.Primary.Delay = 5
SWEP.Primary.Recoil = 0
SWEP.Primary.NumShots = 1
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

//Do NOTHING when player hits reload. This is a sword, not a weapon :D
function SWEP:Reload()
	return false
end

/*function resetSwing()
	swung = false
end*/

function SWEP:Initialize()
	bloody = false
//	self:SetWeaponHoldType("melee")
//	justbloodied = false
end

//Only does stuff when you attack. May add some sort of spark effect if someone pistols/sabres you with your sword out.
function SWEP:Thinks()
//Update view model
	if self.Owner:Team() == TEAM_BLUE then
		if bloody then
			self.Owner.GetViewModel():SetModel("models/sabre/v_sab2e.mdl")
		else
			self.Owner.GetViewModel():SetModel("models/sabre/v_sabre.mdl")
		end
	else
		if bloody then
			self.Owner.GetViewModel():SetModel("models/sabre/v_sab22.mdl")
		else
			self.Owner.GetViewModel():SetModel("models/sabre/v_sabr2.mdl")
		end
	end
end

//Primary function, swing your sword like a pirate!
function SWEP:PrimaryAttack()
	//Do nothing if you're dead
	if self.Owner:Alive() == false then return end
	//Start trace function to find if there is anything within 75 units infront of you
	local tr = {}
	tr.start = self.Owner:GetShootPos()
	tr.endpos = tr.start +(self.Owner:GetAimVector()*75)
	tr.filter = self.Owner
	local trace = util.TraceLine(tr)
	//Make sure we hit something
	if trace.Hit then
		self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)
		if trace.Entity:IsPlayer() || trace.Entity:IsNPC() then //Hit a person/npc >:D
			bloody = true
		end
		self.Weapon:EmitSound("physics/flesh/flesh_impact_bullet"..math.random(3,5)..".wav")	

		bullet = {} //Credit here goes to XcoliahX for his primary fire script of his lightsaber swep
		bullet.Num    = 1
		bullet.Src    = self.Owner:GetShootPos()
		bullet.Dir    = self.Owner:GetAimVector()
		bullet.Spread = Vector(0, 0, 0)
		bullet.Tracer = 0
		bullet.Force  = 0
		bullet.Damage = 25
		self.Owner:FireBullets(bullet)

	else //We missed :(
		self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
		self.Weapon:EmitSound("weapons/iceaxe/iceaxe_swing1.wav")
	end
	self.Owner:SetAnimation( PLAYER_ATTACK1 )
end

//Sets animation for pulling it out
function SWEP:Deploy()
//	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
//	self.Weapon:EmitSound( "weapons/knife/knife_deploy1.wav" )
	return true
end 
    

//Should be for blocking attacks, but won't be yet
function SWEP:SecondaryAttack()
	return false
end

